﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrnMall.Core;
using BrnMall.Core.Domain.WeiXin;
using System.Data;

namespace BrnMall.Data
{
	public partial class WeiXinData
	{
		#region 微信接入设置

		public static void SaveJoinConfig(JoinConfigInfo info)
		{
			BMAData.RDBS.SaveWeiXinJoinConfig(info);
		}

		public static JoinConfigInfo GetJoinConfigInfo()
		{
			var reader = BMAData.RDBS.GetWeiXinJoinConfig();
			if (reader.Read())
			{
				return new JoinConfigInfo
				{
					Id = TypeHelper.ObjectToInt(reader["Id"]),
					Url = reader["Url"].ToString(),
					AppID = reader["AppID"].ToString(),
					AppSecret = reader["AppSecret"].ToString(),
					Token = reader["Token"].ToString(),
					EncodingAESKey = reader["EncodingAESKey"].ToString(),
					EncodingAESMode = (EncodingAESMode)TypeHelper.ObjectToInt(reader["EncodingAESMode"])
				};
			}
			return null;
		}

		#endregion

		#region 微信公众号菜单设置

		public static void SaveWeiXinMenus(WeiXinMenu[] menus)
		{
			BMAData.RDBS.SaveWeiXinMenus(menus);
		}
		public static WeiXinMenu[] GetWeiXinMenus()
		{
			var dt = BMAData.RDBS.GetWeiXinMenus();

			if (dt == null || dt.Rows.Count == 0)
			{
				return new WeiXinMenu[0];
			}
			var menus = new List<WeiXinMenu>();
			foreach (DataRow row in dt.Rows)
			{
				menus.Add(new WeiXinMenu
				{
					Id = TypeHelper.ObjectToInt(row["Id"]),
					Url = row["Url"].ToString(),
					Name = row["Name"].ToString(),
					ParentId = TypeHelper.ObjectToInt(row["ParentId"])
				});
			}

			return menus.ToArray();
		}

		#endregion
	}
}
